package com.cls.business.service.impl;

import com.cls.business.entity.EntrustTopic;
import com.cls.business.entity.Topic;
import com.cls.business.mapper.EntrustTopicMapper;
import com.cls.business.mapper.TopicMapper;
import com.cls.common.entity.QueryRequest;
import com.cls.business.entity.TopicSubjectDetail;
import com.cls.business.mapper.TopicSubjectDetailMapper;
import com.cls.business.service.ITopicSubjectDetailService;
import com.cls.common.enums.DictEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
import java.util.List;

/**
 *  Service实现
 *
 * @author slx
 * @date 2020-11-20 16:58:28
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class TopicSubjectDetailServiceImpl extends ServiceImpl<TopicSubjectDetailMapper, TopicSubjectDetail> implements ITopicSubjectDetailService {
    @Autowired
    private TopicMapper topicMapper;

    @Autowired
    private EntrustTopicMapper entrustTopicMapper;


    @Override
    public PageInfo<TopicSubjectDetail> findTopicSubjectDetailsByTopicId(QueryRequest request, TopicSubjectDetail topicSubjectDetail, String topicClassifyCode) {
        LambdaQueryWrapper<TopicSubjectDetail> queryWrapper = new LambdaQueryWrapper<>();
        // TODO 设置查询条件
        queryWrapper.eq(TopicSubjectDetail::getTopicId,topicSubjectDetail.getTopicId());
        queryWrapper.eq(TopicSubjectDetail::getPlanId,topicSubjectDetail.getPlanId());
//        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<TopicSubjectDetail> list= this.list(queryWrapper);
        for(TopicSubjectDetail one: list){
            if(DictEnum.ANNUAL.getCode().equals(topicClassifyCode)){
                Topic topic = topicMapper.selectById(one.getTopicId());
                if(topic != null){
                    one.setName(topic.getTopicName());
                }
            } else {
               EntrustTopic topic = entrustTopicMapper.selectById(one.getTopicId());
                if(topic != null){
                    one.setName(topic.getTopicName());
                }
            }
        }
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public List<TopicSubjectDetail> findTopicSubjectDetails(TopicSubjectDetail topicSubjectDetail) {
	    LambdaQueryWrapper<TopicSubjectDetail> queryWrapper = new LambdaQueryWrapper<>();
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createTopicSubjectDetail(TopicSubjectDetail topicSubjectDetail) {
        this.save(topicSubjectDetail);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateTopicSubjectDetail(TopicSubjectDetail topicSubjectDetail) {
        this.saveOrUpdate(topicSubjectDetail);
    }


    /**
    * 删除
    * @param ids
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteTopicSubjectDetails(String[] ids) {
        List< String > list = Arrays.asList(ids);
        this.removeByIds(list);
     }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteTopicSubjectDetails(TopicSubjectDetail topicSubjectDetail) {
        LambdaQueryWrapper<TopicSubjectDetail> wrapper = new LambdaQueryWrapper<>();
        // TODO 设置删除条件

        this.remove(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void clearChooseByTopicId(Long topicId) {
        baseMapper.clearChooseByTopicId(topicId);
    }

}
